// pages/food/food.js
import http from '../../utils/http.js'
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图
    imgUrl: [],
    // food侧边导航栏索引
    activeKey: 0,
    // food侧边导航栏数据
    sidebarData:[],
    // foodData,右边数据
    foodData:[],
  },
  
  //空函数，用来阻止冒泡
  null() {
    
  },


  // onChange(e) {
  //   // console.log(e)
  //   // console.log("food数："+e.detail)
  //   // console.log(e.currentTarget.dataset.food.id)
  //   // console.log(e.currentTarget.dataset.food.categoryId)
  //   this.setData({
  //     'sfoodData.num': e.detail,
  //     'sfoodData.id' : e.currentTarget.dataset.food.id,
  //     'sfoodData.categoryId' :e.currentTarget.dataset.food.categoryId,
  //     'sfoodData.food' :e.currentTarget.dataset.food
  //   })
  //   this.updateSelectFoodData(this.data.sfoodData);
  //   console.log("个数："+store.selectFoodData.length)
  //   console.log(store.selectFoodData[0])
  // },


  //减购
  reduceBuy(e) {
    http.postRequest("cater/wxCart/reduceBuy",{
      "openId": store.openId,
      "foodId": e.currentTarget.dataset.foodid,
      "categoryId": e.currentTarget.dataset.categoryid,
      "num": e.currentTarget.dataset.num
    },
    (res)=>{
      console.log(res)
      wx.showToast({
        title: res.data.data,
        icon: 'success',
        duration: 1000 //持续的时间
      })
      this.onChangeSidebar( e.currentTarget.dataset.num,e.currentTarget.dataset.categoryid)
    },'','')
    console.log("减购")
  },
  //加购
  addBuy(e){
    http.postRequest("cater/wxCart/addBuy",{
      "openId": store.openId,
      "foodId": e.currentTarget.dataset.foodid,
      "categoryId": e.currentTarget.dataset.categoryid,
      "num": e.currentTarget.dataset.num
    },
    (res)=>{
      console.log(res)
      wx.showToast({
        title: res.data.data,
        icon: 'success',
        duration: 1000 //持续的时间
      })
      this.onChangeSidebar( e.currentTarget.dataset.num,e.currentTarget.dataset.categoryid)
    },'','')
    console.log("加购")
  },

  //获取store.js里面的数据
  getStoreData(){
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ["openId","sysHeight"],
      actions: ["updateDishData","updateMealData"]
    });
  },

  //点击food查看详情
  detailFood(e){
    if (typeof e.currentTarget.dataset.img == 'undefined') {
      this.detailMeal(e);
    } else {
      this.detailDish(e);
    }
  },

  //detailMeal,查看套餐详情
  detailMeal(e) {
    http.getRequest("cater/meal/detailFood/"+e.currentTarget.dataset.categoryid+'/'+e.currentTarget.dataset.foodid,'',
    (res)=>{
      console.log(res.data.data);
      this.updateMealData(res.data.data)
      wx.navigateTo({
        url: '/pages/meal/meal'
      })
    },'','')
  },

  //detailDish,查看菜品详情
  detailDish(e) {
    http.getRequest("cater/dish/detailFood/"+e.currentTarget.dataset.categoryid+'/'+e.currentTarget.dataset.foodid,'',
    (res)=>{
      this.updateDishData(res.data.data);
      wx.navigateTo({
        url: '/pages/dish/dish'
      })
    },'','')
  },

  // 获取food页面轮播图
  getCarousel() {
    http.getRequest("cater/carousel/findCarousel",'',
    (res)=>{
      this.setData({
        'imgUrl': res.data.data,
      })
    },'','')
  },

  // 获取food侧边导航栏索引数据
  getSidebarData() {
    http.getRequest("cater/category/getCategory",'',(res)=>{
      // console.log(res.data)
      this.setData({
        'sidebarData': res.data.data,
      })
    },'','')
  },
 
  // 封装onChangeSidebar
  privateOnChangeSidebar (event) {
    this.onChangeSidebar(this.data.sidebarData[event.detail].type,this.data.sidebarData[event.detail].id);
  },

  // 侧边导航栏改变,获取右边数据
  onChangeSidebar(type,categoryId) {
    console.log(store.openId)
    http.postRequest("cater/category/findFood",{
      "type": type,
      "categoryId": categoryId,
      "openId" : store.openId
    },
    (res)=>{
      // console.log(res)
      this.setData({
        'foodData': res.data.data,
      })
    },'','')
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCarousel();
    this.getSidebarData();
    this.getStoreData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.storeBindings.destroyStoreBindings()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getCarousel();
    this.getSidebarData();
    console.log("触发food页面下拉刷新")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})